package com.easybuilder.core.schedultask.service.impl;


import com.easybuilder.core.schedultask.dao.TaskDao;
import com.easybuilder.core.schedultask.entity.TaskPO;
import com.easybuilder.core.schedultask.enums.JobStatusEnum;
import com.easybuilder.core.schedultask.service.QuartzManager;
import com.easybuilder.core.schedultask.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskDao taskDao;
    @Autowired
    QuartzManager quartzManager;

    @PostConstruct
    public void init() {
        try {
            initSchedule();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //初始化定时任务
    @Override
    public void initSchedule() throws SchedulerException {
        // 这里获取任务信息数据
        List<TaskPO> jobList = taskDao.list();
        for (TaskPO task : jobList) {
            if (JobStatusEnum.RUNNING.getCode().equals(task.getJobStatus())) {
                quartzManager.addJob(task);
            }
        }
        log.info("定时任务初始化完成，共{}个任务", jobList.size());
    }

    //添加一个任务
    @Override
    public void addTask(TaskPO task) {
        int insert = taskDao.insert(task);
        if (insert > 0) {
            quartzManager.addJob(task);
        }
    }


    //暂停定时任务
    @Override
    public void pause(TaskPO task) {
        try {
            quartzManager.pause(task);
            log.info("定时任务暂停成功！name:{}, group:{}", task.getJobName(), task.getJobGroup());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //恢复定时任务
    @Override
    public void resume(TaskPO task) {
        try {
            quartzManager.resume(task);
            log.info("定时任务恢复成功！name:{}, group:{}", task.getJobName(), task.getJobGroup());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //更新定时任务
    @Override
    public void update(TaskPO task) {
        TaskPO oldTask = taskDao.selectById(task.getId()); //原来任务
        //1.更新至数据库
        int update = taskDao.update(task);
        if (update > 0) {
            if (!task.getCronExpression().equals(oldTask.getCronExpression())) { //如果新旧任务的cron表达式发生改变
                //2.更新任务
                try {
                    quartzManager.updateCron(task);
                    log.info("定时任务更新成功！name:{}, group:{}, old-cron:{}, new-cron:{}", task.getJobName(), task.getJobGroup(), oldTask.getCronExpression(), task.getCronExpression());
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //立即执行一个任务
    @Override
    public void runNow(TaskPO task) throws SchedulerException {
        quartzManager.runNow(task);
    }

    //丢弃任务
    @Override
    public void drop(TaskPO task) {

    }


}
